# sku管理
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, GoodsCategory, SPU, SKUSpecification, SPUSpecification
from meiduo_admin.skus.sku_serialiers import SKUSeriallizer, SKUCategorySeriallizer, GoodSimpleSeriallizer, \
    GoodSpecsSeriallizer
from meiduo_admin.user.my_pagination import MyNumberPagination


# 所有上架商品和增删改查
class SKUmodelSet(ModelViewSet):
    pagination_class = MyNumberPagination
    # queryset = SKU.objects.all().order_by('id')
    serializer_class = SKUSeriallizer

    def get_queryset(self):  # 根据前端过滤数据
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword).order_by('id')  # (name__contains名字包含
        else:
            return SKU.objects.all().order_by('id')



# 新增商品三级分类
class SKUCategories(ListAPIView):
    pagination_class = None
    serializer_class = SKUCategorySeriallizer
    queryset = GoodsCategory.objects.filter(subs=None).all()


# 新增商品SKU分类
class GoodSimple(ListAPIView):
    pagination_class = None
    serializer_class = GoodSimpleSeriallizer
    queryset = SPU.objects.all()


# 新增商品SPU规格
class GoodSpecsView(ListAPIView):
    pagination_class = None
    serializer_class = GoodSpecsSeriallizer

    # queryset = SPU.objects.all()
    def get_queryset(self):
        # 获取正则匹配的参数
        spu_id = self.kwargs.get('spu_id')
        return SPUSpecification.objects.filter(spu_id=spu_id).all()
